﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace 桌面视频录制
{
    [Serializable]
    public class DrawCurve : DrawObject
    {
        protected List<Point> pointList = new List<Point>();
        protected int selectedIndex;
       
        public DrawCurve(Point startPoint)
        {
            this.pointList.Add(startPoint);
        }
        public override void Draw(System.Drawing.Graphics g)
        {
            if (pointList.Count > 2)
            {
                Pen pen = new Pen(this.PenColor);
                pen.Width = this.PenWidth;
                g.DrawCurve(pen, pointList.ToArray());
                pen.Dispose();
            }
        }
        public override void MouseMove(System.Windows.Forms.MouseEventArgs e)
        {
            Point lastPoint = pointList[pointList.Count - 1];
            if (Math.Sqrt(Math.Pow(e.X - lastPoint.X, 2) + Math.Pow(e.Y - lastPoint.Y, 2)) > 5)
            {
                pointList.Add(new Point(e.X, e.Y));
            }
        }
        public override bool JudgeIsSelected(System.Windows.Forms.MouseEventArgs e)
        {
            foreach (Point point in pointList)
            {
                Rectangle rect = Rectangle.Inflate(StaticFunction.GetControlRect(point), 5, 5);
                if (rect.Contains(e.Location))
                    return true;
            }
            return false;
        }
        public override void DrawControlPoint(Graphics g)
        {
            foreach (Point point in pointList)
            {
                StaticFunction.DrawControlRegion(g, point);
            }
        }
        public override void MoveLocation(MouseEventArgs e)
        {
            int dx = e.X - this.MouseDownPoint.X;
            int dy = e.Y - this.MouseDownPoint.Y;
            this.MouseDownPoint = new Point(e.X, e.Y);
            for (int i = 0; i < pointList.Count; i++)
            {
                pointList[i] = new Point(pointList[i].X + dx, pointList[i].Y + dy);
            }
        }
        public override bool JudgeIsInControlRegion(MouseEventArgs e)
        {
            for (int i = 0; i < pointList.Count; i++)
            {
                if (StaticFunction.GetControlRect(pointList[i]).Contains(e.Location))
                {
                    selectedIndex = i;
                    return true;
                }
            }
            return false;
        }
        public override void ChangeSize(MouseEventArgs e)
        {
            int dx = e.X - this.MouseDownPoint.X;
            int dy = e.Y - this.MouseDownPoint.Y;
            this.MouseDownPoint = new Point(e.X, e.Y);
            pointList[selectedIndex] = new Point(pointList[selectedIndex].X + dx, pointList[selectedIndex].Y + dy);
        }
        public override string ToString()
        {
            string printMsg = "[曲线(" + this.pointList[0].X + "," + this.pointList[0].Y + ")]->[ID:" + this.Id + "]";
            return printMsg;
        }
        public override string GetDrawObjectMsg()
        {
            string drawObjectMsg = "曲线->起始位置(" + this.pointList[0].X + "," + this.pointList[0].Y + "),终点位置(" + this.pointList[pointList.Count - 1].X + "," + this.pointList[pointList.Count - 1].Y + "),样本点数:" + pointList.Count + "个";
            return drawObjectMsg;
        }
        public override Rectangle GetRectangle()
        {
            return new Rectangle();
        }
        public override Point[] GetPoints()
        {
            return pointList.ToArray();
        }
        public override bool IsAllInRectangle(Region region)
        {
            foreach (Point point in pointList)
            {
                if (region.IsVisible(point) == false)
                    return false;
            }
            return true;
        }
        public override DrawObject Clone(Point point)
        {
            return new DrawCurve(point);
        }
        public override Point[] GetTextBoxLocation()
        {
            return pointList.ToArray();
        }
    }
}
